package com.mrfechetto.utils;

import java.io.File;
import java.net.URL;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class LoggerUtils
{
   public static final String LOG_PROPERTIES_FILE_A = "/log.properties";
   public static final String LOG_PROPERTIES_FILE_B = "config/log.properties";
   public static void configure()
   {
      new File(System.getProperty("user.home") + File.separator + "Application Data" + File.separator + "Gametime" + File.separator + "Logs").mkdirs();
      URL logPropUrl = null;
      int optionUsed = 0;
      
      if( (logPropUrl = LoggerUtils.class.getResource(LOG_PROPERTIES_FILE_A)) != null)
      {
         PropertyConfigurator.configure(logPropUrl);
         optionUsed++;
      }
      else if( (logPropUrl = LoggerUtils.class.getClassLoader().getResource(LOG_PROPERTIES_FILE_A)) != null)
      {
         PropertyConfigurator.configure(logPropUrl);
         optionUsed++;
      }
      else if( (logPropUrl = ClassLoader.getSystemResource(LOG_PROPERTIES_FILE_B)) != null)
      {
         PropertyConfigurator.configure(logPropUrl);
         optionUsed++;
      }
      else if( (logPropUrl = ClassLoader.getSystemResource(LOG_PROPERTIES_FILE_B)) != null)
      {
         PropertyConfigurator.configure(logPropUrl);
         optionUsed++;
      }
      else
      {
         logPropUrl = ClassLoader.getSystemResource(System.getProperty("unittest.properties"));
         if(logPropUrl != null)
         {
            PropertyConfigurator.configure(logPropUrl);
         }
         else
         {
            PropertyConfigurator.configure(System.getProperty("unittest.properties"));
         }
      }
      
      Logger.getLogger(LoggerUtils.class).info("Logger conf: option used " + optionUsed);
      
   }
   
   public static Logger getLogger(String methodName)
   {
      return Logger.getLogger(LoggerUtils.class.getName() + "." + methodName);
   }
   
}
